package com.jspx.talk.core;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.YesNoType;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.dao.GenericDAO;
import com.jspx.txweb.table.TalkMessage;
import com.jspx.talk.TalkListener;
import java.util.*;

/**
 * Created by chenyuan on 2015-8-11.
 *
 */
public class TalkMessageService extends TalkLogicService {

    private GenericDAO genericDAO;
    private final Map<String, String> fieldErrors = new HashMap<String, String>();
    private final Map<String,TalkListener> cacheEventListeners = Collections.synchronizedMap(new LinkedHashMap<String, TalkListener>());
    public  TalkMessageService() {

    }


    @Ref(name = Environment.genericDAO)
    public void setGenericDAO(GenericDAO genericDAO) {
        this.genericDAO = genericDAO;
    }

    /**
     * 注册监听，提供给ioc使用
     * @param eventListener  监听事件
     */
    public void setListeners(TalkListener[] eventListener)
    {
        for (TalkListener event:eventListener)
        {
            registeredEventListeners(event);
        }
    }


    /**
     *
     * @param event 监听事件
     * @return  注册监听
     */
    public boolean registeredEventListeners(TalkListener event)
    {
        if (cacheEventListeners.containsKey(event.getName())) return false;
        cacheEventListeners.put(event.getName(),event);
        return true;
    }


    /**
     *
     * @param talkMessage 发送的消息
     * @return 发送结果
     */
    public String send(TalkMessage talkMessage)
    {

        for (TalkListener talkEventListener:cacheEventListeners.values())
        {
            if (talkEventListener==null)
            {
                continue;
            }
            if (ERROR.equalsIgnoreCase(talkEventListener.send(talkMessage))&&talkEventListener.hasErrors()) {
                fieldErrors.putAll(talkEventListener.getFieldErrors());
                return ERROR;
            }
        }

        if (talkMessage.checkCompleted())
        {
            talkMessage.setCompleted(YesNoType.YES);
            if (genericDAO.update(talkMessage,new String[]{"completed"})>=0)
            {
                fieldErrors.put(Environment.warningInfo,"状态更新失败");
                return ERROR;
            }
        }
        return SUCCESS;
    }

    public String delete(Collection<TalkMessage> talkMessageList)
    {
        if (talkMessageList==null||talkMessageList.isEmpty())
        {
            getFieldErrors().put(Environment.warningInfo,"无数据删除");
            return ERROR;
        }
        TalkMessage[] talkMessage = new TalkMessage[talkMessageList.size()];
        talkMessage = talkMessageList.toArray(talkMessage);
        return delete(talkMessage);
    }

    /**
     *
     * @param talkMessages 发送的消息
     * @return 发送结果
     */
    public String delete(TalkMessage[] talkMessages)
    {
        for (TalkListener talkEventListener:cacheEventListeners.values())
        {
            if (ERROR.equalsIgnoreCase(talkEventListener.delete(talkMessages))) {
                fieldErrors.putAll(talkEventListener.getFieldErrors());
                return ERROR;
            }
        }
        return SUCCESS;
    }


    /**
     *
     * @return 得到错误信息
     */
    public Map<String, String> getFieldErrors()
    {
        return fieldErrors;
    }

    /**
     *
     * @return  返回判断是否有错误
     */
    public boolean hasErrors()
    {
        return !fieldErrors.isEmpty();
    }

}
